package suzf.top.mall.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_me.*
import org.jetbrains.anko.support.v4.startActivity
import suzf.top.base.ext.loadUrl
import suzf.top.base.ext.onClick
import suzf.top.base.ui.fragment.BaseFragment
import suzf.top.base.utils.AppPrefsUtils
import suzf.top.mall.R
import suzf.top.mall.ui.activity.SettingActivity
import suzf.top.provider.common.ProviderConstant
import suzf.top.provider.common.isLogin
import suzf.top.usercenter.ui.activity.LoginActivity
import suzf.top.usercenter.ui.activity.UserInfoActivity

/**
 *  我的
 *========================
 * Created with AndroidStudio
 * Author:Mr.Su
 * Date:2019-09-19
 * Time:15:31
 * ========================
 */
class MeFragment : BaseFragment(),View.OnClickListener {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        return inflater.inflate(R.layout.fragment_me, null)
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initView()
    }

    private fun initView() {
        mUserIconIv.onClick(this)
        mUserNameTv.onClick(this)
        mSettingTv.onClick(this)
    }

    override fun onStart() {
        super.onStart()
        localData()
    }

    /*
        获取本地数据
     */
    private fun localData() {
        if(isLogin()) {
            val userIcon = AppPrefsUtils.getString(ProviderConstant.KEY_SP_USER_ICON)
            if (userIcon.isNotEmpty()) {
                mUserIconIv.loadUrl(userIcon)
                mUserNameTv.text = AppPrefsUtils.getString(ProviderConstant.KEY_SP_USER_NAME)
            }
        }else {
            mUserIconIv.setImageResource(R.drawable.icon_default_user)
            mUserNameTv.text = getString(R.string.un_login_text)
        }
    }

    override fun onClick(view: View) {
        when(view.id) {
            R.id.mUserIconIv,R.id.mUserNameTv -> {
                if (isLogin()) {
                    startActivity<UserInfoActivity>()
                }else {
                    startActivity<LoginActivity>()
                }
            }
            R.id.mSettingTv -> {
                startActivity<SettingActivity>()
            }
        }
    }

}